package com.jk.controller.quartz;

import com.jk.dto.JsonResult;
import com.jk.dto.account.AccUserDTO;
import com.jk.dto.quartz.QrtzTriggersDTO;
import com.jk.entity.account.AccUser;
import com.jk.entity.quartz.ScheduleJob;
import com.jk.service.quartz.QuartzService;
import com.jk.util.BeanMapper;
import com.jk.util.JsonUtil;
import com.jk.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yanghuan on 2016/1/22.
 */
@Controller
@RequestMapping(value = "/quartz/manage")
public class ManageController {

    @Autowired
    private QuartzService quartzService;

    @RequestMapping("/page")
    public String page() {
        return "quartz/manage/list";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public String getList(QrtzTriggersDTO qrtzTriggersDTO) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("total", quartzService.pageCount(qrtzTriggersDTO));
            map.put("rows", quartzService.selectPage(qrtzTriggersDTO));
            return JsonUtil.toString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new HashMap<String, Object>());
        }
    }

    @RequestMapping(value = "toSave")
    public String add(ScheduleJob scheduleJob, ModelMap modelMap, Integer page, Integer rows) {
        if (null != page) {
            modelMap.addAttribute("pageNumber", page);
        }
        if (null != rows) {
            modelMap.addAttribute("pageNumber", rows);
        }
        try {
            if (!StringUtil.isEmpty(scheduleJob.getJobName())) {
                scheduleJob = quartzService.selectJob(scheduleJob);
            }
            modelMap.addAttribute("scheduleJob", scheduleJob);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "quartz/manage/edit";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(ScheduleJob scheduleJob) {
        try {
            quartzService.saveJob(scheduleJob);
            return JsonUtil.toString(new JsonResult(200, "保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "保存失败"));
        }
    }

    @RequestMapping(value = "stopJob")
    @ResponseBody
    public String stopJob(String json) {
        try {
            List<ScheduleJob> list = JsonUtil.readList(json, ScheduleJob.class);
            for (ScheduleJob scheduleJob : list) {
                quartzService.stopJob(scheduleJob);
            }
            return JsonUtil.toString(new JsonResult(200, "暂停成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "暂停失败"));
        }
    }

    @RequestMapping(value = "resumeJob")
    @ResponseBody
    public String resumeJob(String json) {
        try {
            List<ScheduleJob> list = JsonUtil.readList(json, ScheduleJob.class);
            for (ScheduleJob scheduleJob : list) {
                quartzService.resumeJob(scheduleJob);
            }
            return JsonUtil.toString(new JsonResult(200, "恢复成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "恢复失败"));
        }
    }

    @RequestMapping(value = "triggerJob")
    @ResponseBody
    public String triggerJob(String json) {
        try {
            List<ScheduleJob> list = JsonUtil.readList(json, ScheduleJob.class);
            for (ScheduleJob scheduleJob : list) {
                quartzService.triggerJob(scheduleJob);
            }
            return JsonUtil.toString(new JsonResult(200, "执行成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "执行失败"));
        }
    }

    @RequestMapping(value = "deleteJob")
    @ResponseBody
    public String deleteJob(String json) {
        try {
            List<ScheduleJob> list = JsonUtil.readList(json, ScheduleJob.class);
            for (ScheduleJob scheduleJob : list) {
                quartzService.deleteJob(scheduleJob);
            }
            return JsonUtil.toString(new JsonResult(200, "删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "删除失败"));
        }
    }
}
